<template>
  <div class="balance-component">
    <table style="width: 100%;">
      <tr>
        <th><h3 class="balance-title">我的余额：{{ formattedBalance }}</h3><el-button type="success" style="margin-top:-60px;float: right;width: 100px;height: 50px;">提现</el-button> </th>
      </tr>
    </table>
    <table style="width: 100%;">
      <tr>
        <th>
          <el-button type="text" class="balance-button">今日</el-button>
          <el-button type="text" class="balance-button">本月</el-button>
          <el-button type="text" class="balance-button">本季</el-button>
          <div style="margin-top:-8px;float: right;height: 20px;width: 630px">
            <el-form :model="queryData">
              <el-form-item label="查询时间">
                <el-date-picker
                  v-model="queryData.createdTimeStatus"
                  type="datetime"
                  placeholder="开始时间">
                </el-date-picker>
                --
                <el-date-picker
                  v-model="queryData.createdTimeEnd"
                  type="datetime"
                  placeholder="结束时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="success" style="margin-top:-72px;float: right">查询</el-button>
              </el-form-item>
            </el-form>
          </div>

<!--          value-format="yyyy年MM月dd日 HH:mm:ss"-->
        </th>
      </tr>
    </table>
    <table style="width: 100%;height: 50px;">
      <tr>
        <th>我的收入：{{ formattedIncome }}</th>
        <th>我的支出：{{ formattedExpense }}</th>
      </tr>
    </table>
    <table style="width: 100%;">
      <tr>
        <th style="width: 100%;text-align: center"><h3>订单流水</h3></th>
      </tr>
    </table>

    <table class="transactions-table" >

      <thead>

      <tr>
        <th class="time-header">时间</th>
        <th class="order-id-header">订单流水</th>
        <th class="type-header">交易类型</th>
        <th class="amount-header">金额</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="transaction in recordList" :key="transaction.id" class="transaction-row">
        <td class="time-cell">{{ transaction.createdTime }}</td>
        <td class="order-id-cell">{{ transaction.recordUuid }}</td>
        <td class="type-cell">{{ transaction.recordType }}</td>
        <td class="amount-cell">{{ formattedTransactionAmount(transaction.recordAmount) }}</td>
      </tr>
      </tbody>
    </table>

    <pagination
      style="justify-content: center;"
      v-show="total>0"
      :total="total"
      :page.sync="queryData.pageNum"
      :limit.sync="queryData.pageSize"
      @pagination="getRecordList"
    />
  </div>


</template>

<script>
import { listPurse } from "@/api/system/purse";
import {listRecord} from "@/api/system/record";
import {getInfo} from "@/api/login";

export default {
  name: "Purse",
  data() {
    return {

      balance: 0,
      income: 0,
      expense: 0,
      transactions: [],
      purseList:{},
      recordList:[],
      queryData:{
        purseId:null,
        createdTimeStatus: '',
        createdTimeEnd: '',
        pageNum: 1,
        pageSize: 10,
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      userId:undefined,
    };

  },
  computed: {
    formattedBalance() {
      return `${this.balance} 元`;
    },
    formattedIncome() {
      return `${this.income} 元`;
    },
    formattedExpense() {
      return `${this.expense} 元`;
    },
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    formattedTransactionAmount(amount) {
      return `${amount} 元`;
    },

    getUserInfo(){
      getInfo().then(res=>{
        this.userId=res.data.user.userId;
        this.getPurseList();
      })
    },
    getPurseList(){
      listPurse(this.userId).then(res=>{
        this.purseList=res.data
        this.balance=res.data.pursePrice
        this.queryData=res.data.purseId;
        this.getRecordList()
      })
    },
    getRecordList(){
      listRecord(this.queryData).then(res=>{
        this.recordList=res.rows;
        this.income=0;
        this.expense=0;
        this.total = res.total;
        for (let i = 0; i < res.rows.length; i++) {
          if (res.rows[i].recordStatus===1){
            this.income+=res.rows[i].recordAmount
          }else{
            this.expense+=res.rows[i].recordAmount
          }
        }

      })
    },
    //purseList recordList


    // 表单重置
    reset() {
      this.form = {
        purseId: null,
        userId: null,
        pursePrice: null,
        creationTime: null,
        modifyTime: null,
        purseIntegral: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

  }
};
</script>
<style scoped>
.balance-component {
  justify-content: center;
}
.balance-button {
  font-size: 16px;
  margin-right: 30px;
}
.balance-title {
  font-size: 24px;
  margin-bottom: 1rem;
}

.income-expense {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 1.2rem;
  margin-bottom: 1rem;
}

.transactions-table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 0.5rem;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

th {
  background-color: #f2f2f2;
  font-weight: bold;
}

.transaction-row:nth-child(even) {
  background-color: #f2f2f2;
}

.time-header,
.order-id-header,
.type-header,
.amount-header {
  width: 25%;
}

.time-cell,
.order-id-cell,
.type-cell,
.amount-cell {
  width: 25%;
}
</style>
